﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum MoveStatus
{
    IDLE = 0,
    UP = 1,
    DOWN = 2,
    LEFT = 3,
    RIGHT = 4
};


public class RoleBehaviour : MonoBehaviour
{
    public float speed = 5;

    MoveStatus moveStatus = MoveStatus.IDLE;
    public Animator roleAnimator;
    float animateTime = 0f;

    void Start()
    {
        roleAnimator = GetComponent<Animator>();
    }

    void Update()
    {
        if (moveStatus != MoveStatus.IDLE)
        {
            if(animateTime > 0.3f)
            {
                animateTime = 0f;
                moveStatus = MoveStatus.IDLE;
                roleAnimator.SetBool("moveUp", false);
                roleAnimator.SetBool("moveDown", false);
                roleAnimator.SetBool("moveLeft", false);
                roleAnimator.SetBool("moveRight", false);
                roleAnimator.SetBool("Idle", false);
            }
            else
            {
                animateTime += Time.deltaTime;
            }
        }

        switch (moveStatus)
        {
            case MoveStatus.IDLE:
                roleAnimator.SetBool("moveUp", false);
                roleAnimator.SetBool("moveDown", false);
                roleAnimator.SetBool("moveLeft", false);
                roleAnimator.SetBool("moveRight", false);
                roleAnimator.SetBool("Idle", false);
                GetComponent<SpriteRenderer>().sortingOrder = 0;
                break;
            case MoveStatus.UP:
                roleAnimator.SetBool("moveUp", true);
                roleAnimator.SetBool("moveDown", false);
                roleAnimator.SetBool("moveLeft", false);
                roleAnimator.SetBool("moveRight", false);
                roleAnimator.SetBool("Idle", false);
                GetComponent<SpriteRenderer>().sortingOrder = 0;
                break;
            case MoveStatus.DOWN:
                roleAnimator.SetBool("moveUp", false);
                roleAnimator.SetBool("moveDown", true);
                roleAnimator.SetBool("moveLeft", false);
                roleAnimator.SetBool("moveRight", false);
                roleAnimator.SetBool("Idle", false);
                GetComponent<SpriteRenderer>().sortingOrder = 2;
                break;
            case MoveStatus.LEFT:
                roleAnimator.SetBool("moveUp", false);
                roleAnimator.SetBool("moveDown", false);
                roleAnimator.SetBool("moveLeft", true);
                roleAnimator.SetBool("moveRight", false);
                roleAnimator.SetBool("Idle", false);
                GetComponent<SpriteRenderer>().sortingOrder = 0;
                break;
            case MoveStatus.RIGHT:
                roleAnimator.SetBool("moveUp", false);
                roleAnimator.SetBool("moveDown", false);
                roleAnimator.SetBool("moveLeft", false);
                roleAnimator.SetBool("moveRight", true);
                roleAnimator.SetBool("Idle", false);
                GetComponent<SpriteRenderer>().sortingOrder = 0;
                break;
        }
    }

    public void SetAnimation(MoveStatus status)
    {
        moveStatus = status;
    }

    public void SetSuccessAnimation()
    {
        moveStatus = MoveStatus.IDLE;
        roleAnimator.SetTrigger("BecomeHuman");
    }
}
